<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登陆</title>
    <link rel="stylesheet" href="/static/bootstrap-3.3.7-dist/css/bootstrap.css">
    <script src="/static/jQuery-3.3.1.js"></script>
    <style>
        #vaild_img {
            height: 30px;
            width: 240px;
        }
    </style>
</head>
<body>

<div class="container-fluid">
    <div class="row">
        <div class="col-md-5 col-md-offset-3">
            <h3>登陆</h3>
            <form action="{% url 'login' %}" method="post">
                {% csrf_token %}
                <div class="form-group">
                    <label for="username">用户</label>
                    <input type="text" id="username" class="form-control">
                </div>
                <div class="form-group">
                    <label for="pwd">密码</label>
                    <input type="text" id="pwd" class="form-control">
                </div>
                <div class="form-group">
                    <label for="vaild_check">验证码</label>
                    <div class="row">
                        <div class="col-md-6">
                            <input type="text" id="vaild_check" class="form-control">
                        </div>
                        <div class="col-md-6 vaild_check">
                            <img id="vaild_img" src="{% url 'valid_check' %}" alt="">
                        </div>
                    </div>
                </div>
                <div class="form-group">
                    <button type="button" class="btn btn-info">提交</button>
                    <span id="error"></span>
                </div>

            </form>
        </div>
    </div>
</div>
<script>
    $(".vaild_check").on("click", function () {
        $.ajax({
            url: "{% url 'valid_check' %}",
            type: "get",
            success: function (data) {
                {#$(".vaild_check").children("#vaild_img").remove()#}
                {#$(".vaild_check").html(#}
                {#    '<img id="vaild_img" src="{% url "valid_check" %}">'#}
                {#)#}
                $("#vaild_img").attr("src", "{% url 'valid_check' %}")
            }
        })
    })

    $(".btn").on("click", function () {
        $.ajax({
            url: "{% url 'login' %}",
            type: "post",
            data: {
                "username": $("[id='username']").val(),
                "pwd": $("[id='pwd']").val(),
                "vaild_check": $("[id='vaild_check']").val(),
                "csrfmiddlewaretoken": '{{ csrf_token }}',
            },
            success: function (data) {
                if (data.status == 100) {
                    location.href = data.url
                } else {
                    $("#error").html(data["msg"])
                }
            }
        })
    })
    setTimeout(function () {
        $("#error").html("")
    },3000)
</script>
</body>
</html>